#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t ids[10];
	for (int i = 0; i < 10; i++) {
		pid_t id = fork();
		if (id == 0) {
			// child process
			printf("child process created successfully.....PID:%d\n", getpid());
			sleep(3);
			exit(i); // 将子进程的退出码设置为该子进程 PID 在数组 ids 中的下标
		}
		// father process
		ids[i] = id;
	}
	for (int i = 0; i < 10; i++) {
		int status = 0;
		pid_t ret = waitpid(ids[i], &status, 0);
		if (ret > 0) {
			// wait child success
			printf("wait child process success, PID:%d\n", ids[i]);
			if (WIFEXITED(status)) {
				// child process exit normal
				printf("child process exit code:%d\n", WEXITSTATUS(status));
			}
            if (WIFSIGNALED(status)) {
			    // child process killed by signal                             
			    printf("child process killed by signal %d\n", WTERMSIG(status));
		    }
		}
	}
	return 0;
}